package com.inspector.app.ui.flight

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.inspector.app.BaseApp
import com.inspector.app.databinding.FragmentFlightBinding
import com.inspector.app.di.util.UI
import com.inspector.app.di.vo.event.EventLocationChanged
import com.inspector.app.ui.base.BaseDJFragment
import com.inspector.app.util.LogUtil
import com.mapsforge.map.EfbMap
import com.mapsforge.org.oscim.map.IDJMap
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import javax.inject.Inject

@AndroidEntryPoint
class FlightFragment : BaseDJFragment<FragmentFlightBinding>() {

    @Inject
    lateinit var baseApp: BaseApp
    val viewModel by viewModels<FlightViewModel>()

    private val mapLifecycleObserver by lazy { MapLifecycleObserver() }
    private val statusBarLifecycleObserver by lazy { StatusBarLifecycleObserver() }

    val statusBarItems by lazy { //[飞行状态栏动态设置的数据集合]
        statusBarLifecycleObserver.allItems
    }

    val efbMap: EfbMap get() = mapLifecycleObserver.efbMap
    val dMap: IDJMap get() = mapLifecycleObserver.dMap

    override fun provideViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentFlightBinding {
        return FragmentFlightBinding.inflate(inflater, container, false)
    }

    override fun setupView(view: View, savedInstanceState: Bundle?) {
        UI.registerEvents(this)
        setupMapView()
        setupStatusBar()
    }

    //[加载地图]
    private fun setupMapView() {
        mapLifecycleObserver.setupView(this, viewBinding.vsFlightMap.inflate(), viewBinding.mapControl)
        lifecycle.addObserver(mapLifecycleObserver)
    }

    //[加载状态栏]
    private fun setupStatusBar() {
        statusBarLifecycleObserver.setupView(this, viewBinding)
        lifecycle.addObserver(statusBarLifecycleObserver)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLocationChanged(event: EventLocationChanged) {
        LogUtil.e("地图界面", "EventLocationChanged")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        LogUtil.e("地图界面", "onDestroyView")
        UI.unregisterEvents(this)
    }

    override fun initListener() {
    }

    override fun initData() {
    }

}